/**
 * Created by Administrator on 2017/4/20.
 */

var GamePlayer = require('../../game/player').GamePlayer;

var HandCards = require('./handCards').HandCards;


exports.Card9Player = GamePlayer.extend({
    _handCards: null,       // 手牌
    _prehandcards: null,
    init: function(uid, msg) {
        this._super(uid, msg);
        this._prehandcards = [];
        this._handCards = new HandCards();
    },

    isDealer: function() {
        return this.getIndex() == this.getRoom().getDealerIndex();
    },

    /**
     * 初始化发牌
     * @param cardArr
     */
    initHandCards: function(cardArr) {
        this._handCards.init(cardArr);

        this.sendReplay("onInitHandCards", {
            playerIndex: this.getIndex(),
            cards: this._handCards.getInitCards(),
            remain: this.getRoom().getPublicCards().getRemainNum()
        });
    },

    /**
     * 摸一张牌
     * @param card
     */
    addNewCard: function(card) {
        this._handCards.addNewCard(card);
        logger.debug("room=%d index=%d addNewCard card=%d cards=%j", this.getRoom().rid(), this.getIndex(), card, this._handCards.getCards());
    },

    getHandCards: function() {
        return this._handCards;
    },

    getHandCardNum: function(card) {
        return this._handCards.getCardNum(card);
    },
    /******************************************************************
     * 重载的接口
     ******************************************************************/

    onStartNewRound: function() {
        this._super();
        this._handCards.reset();
    },
    settlement: function() {
        this._super();
        this._prehandcards = [];

    },

    /******************************************************************
     * 需要重载的接口
     ******************************************************************/

    /**
     * 初始手牌需要的张数
     * @param playerIndex
     * @returns {number}
     */
    getInitHandCardNeedNum: function() {
        return 5;
    },

    /**
     * 初始化手牌时默认牌
     * @returns {array}
     */
    getInitHandCardPreCards: function() {
        var preCards = [];
        if(this._prehandcards){
            for(var i = 0; i < this._prehandcards.length; i++){
                preCards.push( this._prehandcards[i]);
            }
        }
        //logger.debug("room=%d getInitHandCardPreCards", room.rid());
        return preCards;
    },
    setInitHandCardPreCards: function(cards) {
        if(cards){
            var room = this.getRoom();

            for(var i = 0; i < cards.length; i++){
                this._prehandcards.push(cards[i]);
            }
            logger.debug("room=%d noSetInitHandCardPreCards %j,", room.rid(), cards);
        }
    },
});